﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：车辆管理
 */
var selectedRow;
var refreshGirdData;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加车辆档案',
                    url: config.webportal_server + 'pages/system/car/form.html?access_token=' + config.getToken().access_token,
                    width: 900,
                    height: 640,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑车辆档案',
                        url: config.webportal_server + 'pages/system/car/form.html?access_token=' + config.getToken().access_token,
                        width: 900,
                        height: 640,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'cars/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });

            // 从excel导入
            $('#lr_import').on('click', function () {
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传文件',
                    url: config.webportal_server + 'pages/system/car/uploadifyForm.html?access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (datas) {
                            if (datas) {
                                refreshGirdData();
                            }
                        });
                    }
                });
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'cars/exportExcel?access_token=' + config.getToken().access_token;
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'cars/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '车牌号', name: 'licensePlateNumber', width: 100, align: 'center'},
                    {label: '品牌', name: 'brand', width: 100, align: 'center'},
                    {label: '型号', name: 'serial', width: 150, align: 'left'},
                    {label: '类型', name: 'type', width: 100, align: 'center'},
                    {label: '颜色', name: 'color', width: 60, align: 'center'},
                    {label: '座位', name: 'seat', width: 60, align: 'center'},
                    {label: '里程', name: 'mileage', width: 100, align: 'left'},
                    {label: '驾驶员', name: 'driverName', width: 100, align: 'center'},
                    {label: '所属部门', name: 'departmentName', width: 150, align: 'left'},
                    {
                        label: "状态", name: "status", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            if (cellvalue == 1) {
                                return "可用";
                            }
                            if (cellvalue == 2) {
                                return '<span style=\"color:red;\">外出</span>';
                            }
                            return "";
                        }
                    },
                    {
                        label: "更新时间", name: "updateTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {
                        label: "", name: "btn1", width: 50, align: "left",
                        formatter: function (value, row, op, $cell) {
                            $cell.on('click', '.backcar', function () {
                                if ($cell.html().trim() == '') {
                                    return false;
                                }
                                var keyvalue = $($cell[0].innerHTML).attr("data-value");
                                var licensePlateNumber = $($cell[0].innerHTML).attr("licensePlateNumber");
                                learun.layerForm({
                                    id: 'form',
                                    title: '回车【' + licensePlateNumber + "】",
                                    url: config.webportal_server + 'pages/system/car/back.html?keyValue=' + keyvalue + '&access_token=' + config.getToken().access_token,
                                    width: 980,
                                    height: 760,
                                    callBack: function (id) {
                                        return top[id].acceptClick(refreshGirdData);
                                    }
                                });
                                return false;
                            });
                            if (row.status == 2) {
                                return '<span class="label label-info backcar" data-value="' + row.id + '" licensePlateNumber="' + row.licensePlateNumber + '" style="cursor: pointer;">回车</span>';
                            } else {
                                return '&nbsp;';
                            }
                        }
                    }
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑车辆档案',
                            url: config.webportal_server + 'pages/system/car/form.html?access_token=' + config.getToken().access_token,
                            width: 900,
                            height: 640,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });

            page.search();
        },
        search: function (param) {
            param = param || {};
            param.status = 2;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


